<?php
/* PingControllerClass
 * 
 * This class handles all specific controls and all databasecommunication.
 * 
 * @version 1.0
 * @author      Daniel Eliasson Stilero AB - http://www.stilero.com
 * @copyright	Copyright (c) 2011 Stilero AB. All rights reserved.
 * @license	GPLv2
* 	Joomla! is free software. This version may have been modified pursuant
* 	to the GNU General Public License, and as distributed it includes or
* 	is derivative of works licensed under the GNU General Public License or
* 	other free or open source software licenses.
 *
 *  This file is part of AutoSocialShare. 
 * 
 *     AutoSocialShare is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AutoSocialShare is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AutoSocialShare.  If not, see <http://www.gnu.org/licenses/>.
 */

class PingControllerClass extends stlShareControllerClass{
    const HTTP_STATUS_FOUND = 302; //All ok but prefers other options
    const HTTP_STATUS_OK = 200; //Returned on all ok
    const HTTP_STATUS_FORBIDDEN = 403; //Returned from PingFM on duplicate tweets
    var $fullMessage;
    var $PingFMClass;

    function __construct($config) {
        parent::__construct($config);
        $this->config = array_merge(  
            array(
            'basicPingFMClassFile'   =>     'basicPingFMClass',
            'username'       =>      '',
            'password'       =>      '',
            'serviceToUse'  =>      '',
            'articlesNewerThan'     =>      '',
            'sectionsToTweet'       =>      '',
            'pingDelay'            =>      '',
            'showDebugInfo'         =>      0,
            'useMetaAsHashTag'      =>      ''
            ),
        $config
        );
    }
    
    public function shareToPingFM() {
        $this->buildFullMessage();
        if( $this->error != FALSE) {
            return false;
        }
        $returnCode = $this->doPing();
        return $this->handleReturnCode($returnCode);
    }
    
    private function handleReturnCode($returnCode){
        if( $returnCode == self::HTTP_STATUS_OK ){
            $this->saveLogToDB();
            return true;
        }  else {
            $this->error['message'] = $this->config['pluginLangPrefix'].'ERRORCODE_'.$returnCode;
            $this->error['type'] = 'error';
            return false;
        }
    }
    private function doPing() {
        $this->initializeClasses();
        if($this->error != FALSE ){
            $this->error['message'] = $this->config['pluginLangPrefix'].'FAILED_INIT_CLASSES';
            $this->error['type'] = 'error';
            return;
        }
        $code = $this->PingFMClass->sendPingMessage($this->fullMessage);
        return $code;
    }
        
    private function initializeClasses() {
        if($this->error != FALSE ){
            return;
        }
       $className = $this->config['basicPingFMClassFile'];
       $this->PingFMClass = new $className( 
            array(
            'username'   =>      $this->config['username'],
            'password'   =>      $this->config['password']
            )
        ); 
    }
    
    public function isServerSupportingRequiredFunctions(){
        if($this->error != FALSE ){
            return FALSE;
        }
        if( ! function_exists( 'curl_init' ) || ! function_exists('file_get_contents') ){
            $this->error['message'] = $this->config['pluginLangPrefix'].'NO_CURL_SUPPORT';
            $this->error['type'] = 'error';
            return FALSE;
        }
    }
    
    public function isServerSafeModeDisabled (){
        if(ini_get('safe_mode')){
            $this->error['message'] = $this->config['pluginLangPrefix'].'SERVER_IN_SAFE_MODE';
            $this->error['type'] = 'error';
            return FALSE;
        }
    }

    public function isLoginDetailsEntered() {
        if($this->error != FALSE ){
            return FALSE;
        }
        if( $this->config['username'] == "" || $this->config['password'] == ""){
            $this->error['message'] = $this->config['pluginLangPrefix'].'NOLOGINDETAILS';
            $this->error['type'] = 'error';
            return FALSE;
        }
    }

    private function buildFullMessage() {
        $articleURL = $this->articleObject->full_url;
        $articleTitle = $this->articleObject->title;
        $fullMessage = $articleTitle." ".$articleURL;
        $this->fullMessage = $fullMessage;
    }

    public function setTwitterLoginDetails($twitterUsername, $twitterPassword) {
        $this->config['username'] = $twitterUsername;
        $this->config['password'] = $twitterPassword;
    }
}

